﻿/****************************************************************
  Author:  Ievgenii Nazaruk, ievgenii.nazaruk@gmail.com 
 License:  Microsoft Public License (Ms-PL)
          ( http://www.opensource.org/licenses/ms-pl.html or
            http://www.microsoft.com/opensource/licenses.mspx )
*****************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.Serialization;

namespace Frontend
{
    public partial class ClassTypeFilterPanel : UserControl, IStateSerializable
    {
        public ClassTypeFilterPanel()
        {
            UseManualFilter = false;
            InitializeComponent();            
        }

        public static readonly DependencyProperty UseManualFilterProperty;
        public static readonly DependencyProperty HighlightClubProperty;
        public static readonly DependencyProperty HighlightGroupProperty;
        public static readonly DependencyProperty HighlightGrammarProperty;
        public static readonly DependencyProperty HighlightReadingProperty;
        public static readonly DependencyProperty HighlightWritingProperty;
        public static readonly DependencyProperty HighlightConversationProperty;
        public static readonly DependencyProperty HighlightOtherProperty;

        static ClassTypeFilterPanel()
        {
            string[] propertyNames = 
            { 
              "UseManualFilter",
              "HighlightClub",
              "HighlightGroup",
              "HighlightGrammar",
              "HighlightReading",
              "HighlightWriting",
              "HighlightConversation",
              "HighlightOther"
            };

            DependencyProperty[] properties = new DependencyProperty[propertyNames.Length];

            for (int i = 0; i < propertyNames.Length; i++)
            {
                FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();
                metadata.DefaultValue = true;
                properties[i] = DependencyProperty.Register(propertyNames[i],
                                                            typeof(bool),
                                                            typeof(ClassTypeFilterPanel),
                                                            metadata);

            }

            UseManualFilterProperty  = properties[0];
            HighlightClubProperty    = properties[1];
            HighlightGroupProperty   = properties[2];
            HighlightGrammarProperty = properties[3];
            HighlightReadingProperty = properties[4];
            HighlightWritingProperty = properties[5];
            HighlightConversationProperty = properties[6];
            HighlightOtherProperty = properties[7];
        }

        public bool UseManualFilter
        {
            get
            {
                return (bool)GetValue(UseManualFilterProperty);
            }
            set
            {
                SetValue(UseManualFilterProperty, value);
            }
        }

        public bool HighlightClub
        {
            get
            {
                return (bool)GetValue(HighlightClubProperty);
            }
            set
            {
                SetValue(HighlightClubProperty, value);
            }
        }

        public bool HighlightGroup
        {
            get
            {
                return (bool)GetValue(HighlightGroupProperty);
            }
            set
            {
                SetValue(HighlightGroupProperty, value);
            }
        }

        public bool HighlightGrammar
        {
            get
            {
                return (bool)GetValue(HighlightGrammarProperty);
            }
            set
            {
                SetValue(HighlightGrammarProperty, value);
            }
        }

        public bool HighlightReading
        {
            get
            {
                return (bool)GetValue(HighlightReadingProperty);
            }
            set
            {
                SetValue(HighlightReadingProperty, value);
            }
        }

        public bool HighlightWriting
        {
            get
            {
                return (bool)GetValue(HighlightWritingProperty);
            }
            set
            {
                SetValue(HighlightWritingProperty, value);
            }
        }

        public bool HighlightConversation
        {
            get
            {
                return (bool)GetValue(HighlightConversationProperty);
            }
            set
            {
                SetValue(HighlightConversationProperty, value);                
            }
        }

        public bool HighlightOther
        {
            get
            {
                return (bool)GetValue(HighlightOtherProperty);
            }
            set
            {
                SetValue(HighlightOtherProperty, value);
            }
        }

        private void UseManualFilterCheckbox_Checked(object sender, RoutedEventArgs e)
        {
            Expander.IsExpanded = true;            
        }

        /// <summary>
        /// Fired whenever any of the properties of filter is changed.
        /// </summary>
        public event EventHandler<EventArgs> Changed;

        protected void OnChanged()
        {
            EventArgs args = new EventArgs();
            if (Changed != null)
            {
                Changed(this, args);
            }
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            OnChanged();
        }


        public StateDictionary GetState()
        {
            StateDictionary state = new StateDictionary();
            state["IsExpanded"] =  Expander.IsExpanded;
            state["UseManualFilter"] = UseManualFilter;
            state["HighlightClub"] = HighlightClub;
            state["HighlightConversation"] = HighlightConversation;
            state["HighlightGrammar"] = HighlightGrammar;
            state["HighlightGroup"] = HighlightGroup;
            state["HighlightReading"] = HighlightReading;
            state["HighlightWriting"] = HighlightWriting;
            state["HighlightOther"] = HighlightOther;

            return state;
        }

        public void SetState(StateDictionary state)
        {
            Expander.IsExpanded = state.GetBoolean("IsExpanded");
            UseManualFilter = state.GetBoolean("UseManualFilter");
            HighlightClub = state.GetBoolean("HighlightClub");
            HighlightConversation = state.GetBoolean("HighlightConversation");
            HighlightGrammar = state.GetBoolean("HighlightGrammar");
            HighlightGroup = state.GetBoolean("HighlightGroup");
            HighlightReading = state.GetBoolean("HighlightReading");
            HighlightWriting = state.GetBoolean("HighlightWriting");
            HighlightOther = state.GetBoolean("HighlightOther");
        }

        private void ClearAll_Click(object sender, RoutedEventArgs e)
        {
            HighlightClub = false;
            HighlightConversation = false;
            HighlightGrammar = false;
            HighlightGroup = false;
            HighlightReading = false;
            HighlightWriting = false;
            HighlightOther = false;
        }

        private void SelectAll_Click(object sender, RoutedEventArgs e)
        {
            HighlightClub = true;
            HighlightConversation = true;
            HighlightGrammar = true;
            HighlightGroup = true;
            HighlightReading = true;
            HighlightWriting = true;
            HighlightOther = true;
        }
    }
}
